/************init.html************/

function queryEmp_emp(page) {
	sys_loading();
	var parm = $('#queryEmpForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('emp-query');
	sys_record_parm('emp',parm);
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#empResultList').html(result);
		}
	});
}

function deleteEmp_emp(code, realName) {
	var url = '${contextPath}emp-delete.html?time=' + getTime();
	$.Zebra_Dialog('要删除 \'' + realName + '\' 雇员吗?',{
	'type': 'question',
	'title': '警告' ,
	'buttons':[
	           {caption:'确认',callback:function() {
	        	   aidExecuteUser_emp(code, url);
	    		}},
	           {caption:'取消',callback:function(){}
	    	}]
	});
}

function unshieldEmp_emp(code, realName) {
	$.Zebra_Dialog('确认要解除 ' + name + ' 机构的屏蔽状态吗？',{
	'type': 'question',
	'title': '警告' ,
	'buttons':[
	           {caption:'确认',callback:function() {
	        	   	var url = create_url('emp-unshield');
	   				aidExecuteUser_emp(code, url);
	    		}},
	           {caption:'取消',callback:function(){}
	    	}]
	});
}

function shieldEmp_emp(code, realName) {
	$.Zebra_Dialog('确定是否屏蔽'+name+' 用户的信息吗？',{
	'type': 'question',
	'title': '警告' ,
	'buttons':[
	           {caption:'确认',callback:function() {
        	   		var url = create_url('emp-shield');
   					aidExecuteUser_emp(code, url);
	    		}},
	           {caption:'取消',callback:function(){}
	    	}]
	});
}

function aidExecuteUser_emp(code, url) {
	var parm = {'code':code};
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				queryEmp_emp();
			} else {
				sys_msg('操作失败，请刷新页面再次尝试，如果问题依然存在，请联系管理员!');
			}
		}
	});
}

/**
 * 通过机构编号查询出该机构和该机构所有的子机构
 */
function selectOrgan_emp(oc, on) {
	var url = create_url('emp-aidInit');
	var parm = {'oc':oc, 'on':on};
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'800px', height:'600px', open:true});
}

/*************edit.html**************/

/**
 * 验证Email地址
 * @param {} email
 * @return {Boolean}
 */
function s_validateEmail(email) {
	var reEmail = /^([A-Za-z0-9])(\w)+@(\w)+(\.)(com|com\.cn|net|cn|net\.cn|org|biz|info|gov|gov\.cn|edu|edu\.cn)/;
	return email.match(reEmail) || email == " ";
}

function saveEmp_emp() {
	// 邮箱格式
	var email = $('#email').val();
	if (!s_validateEmail(email)) {
		sys_msg('对不起！该邮箱名【' + email + '】格式不正确', '警告');
		return false;
	}
	var parm = $('#editEmpForm').serialize();
	var url = create_url('emp-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if (result == 'success') {
				sys_msg('保存成功！');
				sys_load_handle('emp');
			} else if (result == 'exists') {
				sys_msg('对不起！该邮箱名【' + email + '】已经被使用！');
			} else if (result == 'parameter_error') {
				sys_msg('错误提示', '保存失败，请选择该用户所属机构', '提示');
			} else if (result == 'null_oc') {
				sys_msg('错误提示', '该账号没有分配机构，请先分配机构在操作');
			} else if (result == 'no_any_rights') {
				$.Zebra_Dialog('请尽快给该用户赋予角色，否则该用户不能正常登录', '提示', function(r) {
					if(r == true){
						sys_load_handle('emp');
					}
				});
			} else {
				sys_msg('操作失败');
			}
		}
	});
}

function selectOrgan_emp(oc, on) {
	var url = create_url('emp-aidInit');
	var parm = {'oc': oc, 'on': on};
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'800px', height:'600px', open:true});
}

function deleteSelectedItem_emp(btn) {
	btn.parents('.rep').remove();
}

function download_emp(path, code) {
	window.location.href = path + 'm/emp-download_' + code + ".html";
}

function addFilesUploadEvent_emp() {
	var url = create_url('emp-uploads');
	var btn = $('#uploadFilesBtn');
	new AjaxUpload(btn, {
		action:url,
		name:'files',
		multiple: true,
		responseType: 'json',
		onSubmit : function(file, ext) {
			if (ext && /^(zip|rar|txt|doc|docx|xls|xlsx|ppt|pptx|pdf|jpg|png|jpeg|gif|bmp|ZIP|RAR|TXT|DOC|DOCX|XLS|XLSX|PPT|PPTX|PDF|JPG|PNG|JPEG|GIF|BMP)$/.test(ext)){
				this.setData({
					'info':'文件类型正确'
				});
			} else {
				sys_warning('文件类型错误', '只能上传类型为：zip、rar、txt、doc、docx、xls、xlsx、ppt、pptx、pdf、及图片文件!');
				return false;               
			}
			$('#uploads_span').html(sys_loading_img);
			this.disable();
		},
		onComplete: function(files, response){
			
			if ('error' == response.result)
				sys_warning('失败', '上传文件失败，请检查文件名是否正确!');
			else if ('file_type_error' == response.result)
				sys_warning('文件类型错误', '只能上传类型为：JGP、GIF、BMP、ICO、PNG的文件!');
			else {
				var template = $('#tempAnnexListDiv').find('.rep');
				var annexList = $('#annexListTd');
				var index = parseInt($('#mail_an_index').val());
				$.each(response.list, function(i) {
					var pii = template.clone(true);
					pii.find('.filename').html(this.name).end()
						.find('.name').attr('name', 'anList[' + (index) + '].name').val(response.list[i].name).end()
						.find('.size').attr('name', 'anList[' + (index) + '].size').val(response.list[i].size).end()
						.find('.uuid').attr('name', 'anList[' + (index) + '].uuid').val(response.list[i].uuid).end()
						.find('.size').attr('name', 'anList[' + (index) + '].size').val(response.list[i].size).end()
						.find('.ext').attr('name', 'anList[' + (index) + '].ext').val(response.list[i].ext).end()
						.find('.uid').attr({'src':sys_context_path + "upload/userAnnex/" + response.list[i].uuid + "." + response.list[i].ext}).end();
					annexList.append(pii);
					index ++;
				});
				this.enable();
				$('#mail_an_index').val(index);
			}  
		}
	});
}

